var express = require('express')
const { format } = require('express/lib/response')
const res = require('express/lib/response')
var router = express.Router()

const { DEBUG } = require('../config/config.default')

// 导入db, 操作数据库
const { getAll, getById, exec } = require('../db')

/* 获取所有 */
router.get('/', async (req, res) => {
  const { user_type, page, size } = req.query
  if (!user_type && !page) {
    let sql = 'select * from gg_user'
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: '获取所有用户成功',
      result: data,
    })
  } else if (page && size && user_type) {
    // 分页操作
    let offset = (page - 1) * size
    // 判断用户类型：管理员 交通部门 普通用户，
    let sql1 = `select count(*) as total from gg_user where user_type = '${user_type}'`
    const data1 = await getById(sql1)
    let sql = `select * from gg_user where user_type = '${user_type}' limit ${offset},${size} `
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: '获取该页数据成功',
      result: {
        total: data1.total,
        data,
      },
    })
  }
})

/* 获取单个用户 */
router.get('/:user_id', async (req, res) => {
  const { user_id } = req.params

  let sql = `select * from gg_user where user_id = ${user_id} and user_other = 1`

  const data = await getById(sql)
  res.send({
    code: 0,
    message: '获取单个用户成功',
    result: data,
  })
})

/* 新增用户 */
router.post('/', async (req, res) => {
  const { user_name, user_password, user_type } = req.body
  if (user_type) {
    let sql = `insert into gg_user (user_name,user_password,user_type) values ('${user_name}','${user_password}','${user_type}') `
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '添加用户成功',
      result: {
        user_id: data.insertId,
        user_name,
        user_type,
        user_password,
      },
    })
  } else {
    let sql = `insert into gg_user (user_name,user_password) values ('${user_name}','${user_password}') `
    const data = await exec(sql)
    res.send({
      code: 0,
      message: '添加用户成功',
      result: {
        user_id: data.insertId,
        user_name,
        user_password,
      },
    })
  }
})

/* 修改用户 */
router.put('/:user_id', async (req, res) => {
  const { user_id } = req.params
  const { user_password, user_type, user_name, user_onlinestatus } = req.body

  if (!user_type && !user_name && user_password) {
    // 修改用户密码
    let sql = `update gg_user set user_password='${user_password}' where user_id = ${user_id} `

    await exec(sql)
    res.send({
      code: 0,
      message: '修改密码成功',
      result: {
        user_id,
        user_password,
      },
    })
  } else if (user_id && user_name && user_password) {
    // 修改用户名 密码  登录状态
    let sql = `update gg_user set user_name='${user_name}',user_password = '${user_password}',user_onlinestatus=${user_onlinestatus} where user_id = ${user_id} `
    console.log(sql)
    await exec(sql)
    res.send({
      code: 0,
      message: '修改用户信息成功',
      result: {
        user_id,
        user_name,
        user_password,
        user_onlinestatus,
      },
    })
  } else if (user_type && user_id) {
    // 更新用户类型  管理 / 普通
    let sql = `update gg_user set user_type='${user_type}' where user_id = ${user_id} `
    console.log(sql)
    await exec(sql)
    res.send({
      code: 0,
      message: '修改用户类型成功',
      result: {
        user_id,
        user_type,
      },
    })
  } else {
    // 修改用户登录状态
    let sql = `update gg_user set user_onlinestatus=${user_onlinestatus} where user_id = ${user_id} `
    console.log(sql)
    await exec(sql)
    res.send({
      code: 0,
      message: '修改用户登录状态成功',
      result: {
        user_id,
        user_onlinestatus,
      },
    })
  }
})

/* 删除用户 */
/*  */
router.delete('/:user_id', async (req, res) => {
  const { user_id } = req.params
  const { id, flag } = req.query
  console.log(req.query)
  if (!id && !flag) {
    let sql = `update gg_user set user_other =0 where user_id = ${user_id}`
    await exec(sql)
    res.send({
      code: 0,
      message: '删除成功',
      result: '',
    })
  } else if (id) {
    let sql = `update gg_user set user_other = 1 where user_id = ${id}`
    await exec(sql)
    res.send({
      code: 0,
      message: '删除成功',
      result: '',
    })
  } else if (flag) {
    // 外键约束  要删一起删才行
    let sql = `delete from gg_event where user_id = ${user_id}`
    await exec(sql)
    console.log('在这里', sql)
    let sql1 = `delete from gg_notice where user_id = ${user_id}`
    await exec(sql1)
    // 最后才能删除这张表的对应内容
    let sql2 = `delete from gg_user where user_id = ${user_id}`
    await exec(sql2)
    res.send({
      code: 0,
      message: '彻底删除成功',
      result: '',
    })
  }
})

module.exports = router
